<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\FestivalModel;
use app\admin\model\ContentModel;

class FestivalController extends AdminBaseController{
    
    public function index(){
        $model = new FestivalModel();
        $categoryTree        = $model->festivalTableTree(array());
        
        $this->assign('category_tree', $categoryTree);
        return $this->fetch();
    }
    
    public function add(){
        $parentId            = $this->request->param('parent', 0, 'intval');
        $model = new FestivalModel();
        $categoriesTree      = $model->festivalTree($parentId);
        
        $this->assign('categories_tree', $categoriesTree);
        return $this->fetch();
    }
    
    public function addPost()
    {
        $model = new FestivalModel();
    
        $post = $this->request->param();
        
        $data['parent_id'] = $post['parent_id'];
        $data['name'] = $post['name'];
        $data['date'] = $post['date'];
        $data['image'] = $post['image'];
        $data['add_time'] = time();
        
        $order = $model->where('parent_id', $post['parent_id'])->max('list_order');
        $data['list_order'] = $order + 1;
    
        $result = $model->data($data)->save();
    
        if ($result === false) {
            $this->error('添加失败!');
        }
     
        $this->success('添加成功!', url('Festival/index'));
    
    }
    
    
    public function edit(){
        $id = $this->request->param('id', 0, 'intval');
        $category = FestivalModel::get($id)->toArray();
        $this->assign($category);
        $model = new FestivalModel();
        $categoriesTree      = $model->festivalTree($category['parent_id'], $id);
        
        $this->assign('categories_tree', $categoriesTree);
        
        return $this->fetch();
    }
    
    
    public function editPost(){

        $post = $this->request->param();
        
        $data['parent_id'] = $post['parent_id'];
        $data['name'] = $post['name'];
        $data['date'] = $post['date'];
        $data['image'] = $post['image'];
        
        $model = new FestivalModel();
        
        $result = $model->save($data,['id' => $post['id']]);
        
        if ($result === false) {
            $this->error('保存失败!');
        }
        
        $this->success('保存成功!', url('Festival/index'));
        
    }
    
    public function delete(){

        $model = new FestivalModel();
        $id                  = $this->request->param('id');
        //获取删除的内容
        $findCategory = $model->where('id', $id)->find();
        
        if (empty($findCategory)) {
            $this->error('分类不存在!');
        }
        
        $categoryChildrenCount = $model->where('parent_id', $id)->count();
        
        if ($categoryChildrenCount > 0) {
            $this->error('此分类有子类无法删除!');
        }
        
        $result = $model
        ->where('id', $id)
        ->update(['is_del' => 1]);
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
        
    }
    
    /**
     * 内容页
     */
    public function content(){
        $param = $this->request->param();

        $categoryId = $this->request->param('category', 0, 'intval');

        $fmodel = new festivalModel();
        $categoryTree        = $fmodel->festivalTree($categoryId);
        
        $model = new ContentModel();
        $data = $model->where('is_del', 0)->order('fid DESC, id DESC')->select()->toArray();
        foreach ($data as $k => &$v){
            $festival = $fmodel->where('id', $v['fid'])->find();
            $v['parent'] = $fmodel->where('id', $festival['parent_id'])->value('name');
            $v['title'] = $festival['name'];
        }

        $this->assign('data', $data);
        $this->assign('category_tree', $categoryTree);
        $this->assign('category', $categoryId);

        return $this->fetch();
    }
    
    /**
     * 添加内容
     */
    public function addContent(){
        
        return $this->fetch('add_content');
    }
    
    public function addContentPost(){
        $post = $this->request->param();
        $data['fid'] = $post['fid'];
        $data['image1'] = $post['image1'];
        $data['image2'] = $post['image2'];
        $data['image3'] = $post['image3'];
        $data['image4'] = $post['image4'];
        $data['image5'] = $post['image5'];
        $data['image6'] = $post['image6'];
        $data['add_time'] = time();
        
        $model = new ContentModel();
        $res = $model->data($data)->save();
        if ($res === false) {
            $this->error('添加失败!');
        }
     
        $this->success('添加成功!', url('Festival/content'));
    }
    
    public function editContent(){
        $id = $this->request->param('id', 0, 'intval');
        $model = new ContentModel();
        $data = $model->where('id', $id)->find();
        
        $fmodel = new FestivalModel();
        $festival = $fmodel->where('id', $data['fid'])->find();
        
        $this->assign('data', $data);
        $this->assign('festival', $festival);
        
        return $this->fetch();
    }
    
    
    public function editContentPost(){
        $post = $this->request->param();
        
        $data['fid'] = $post['fid'];
        $data['image1'] = $post['image1'];
        $data['image2'] = $post['image2'];
        $data['image3'] = $post['image3'];
        $data['image4'] = $post['image4'];
        $data['image5'] = $post['image5'];
        $data['image6'] = $post['image6'];
        
        $model = new ContentModel();
        
        $result = $model->save($data,['id' => $post['id']]);
        
        if ($result === false) {
            $this->error('保存失败!');
        }
        
        $this->success('保存成功!', url('Festival/content'));
    }
    
    /**
     * 分类选择
     */
    public function select(){

        $ids                 = $this->request->param('ids');
        $selectedIds         = explode(',', $ids);
        $model = new FestivalModel();
        
        $tpl = <<<tpl
<tr class='data-item-tr'>
    <td>
        <input type='radio' class='js-check' data-yid='js-check-y' data-xid='js-check-x' name='ids'
               value='\$id' data-name='\$name' \$checked \$disabled>
    </td>
    <td>\$id</td>
    <td>\$spacer \$name</td>
</tr>
tpl;
        
        $categoryTree = $model->festivalTableTree($selectedIds, $tpl);
        
        $where      = array();
        $categories = $model->where($where)->select();
        
        $this->assign('categories', $categories);
        $this->assign('selectedIds', $selectedIds);
        $this->assign('categories_tree', $categoryTree);
        return $this->fetch();
        
    }
    
}